package com.tjtt.tdm.base;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tjtt.tdm.exception.SystemException;

public class BaseHibernateDao extends HibernateDaoSupport {

	/**
	 * 根据HQL语句进行分页查询
	 * @param hql HQL语句
	 * @param params HQL语句带的多个参数值
	 * @param offset 从第几条记录开始查询
	 * @param pagesize 每页显示多少行
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageResult searchPaginated(String hql, Map filterMap){
		int page = (Integer) filterMap.get("page");
		int rows = (Integer) filterMap.get("rows");
		int start = rows * ( page - 1); //开始的记录
		//获取记录总数
		String countHql = getCountQuery(hql);
		Query query = getSession().createQuery(countHql);
		int total =((Number) query.uniqueResult()).intValue();
		
		//获取当前页的结果集
		query = getSession().createQuery(hql);
		
		query.setFirstResult(start);
		query.setMaxResults(rows);
		List<?> datas = query.list();
		
		PageResult pm = new PageResult();
		pm.setRowCount(total);
		pm.setData(datas);
		return pm;
	}
	
	/**
	 * 根据HQL语句，获得查找总记录数的HQL语句
	 * 如：
	 * select ... from Orgnization o where o.parent is null
	 * 经过转换，可以得到：
	 * select count(*) from Orgnization o where o.parent is null
	 * @param hql
	 * @return
	 */
	private String getCountQuery(String hql){
		int index = hql.indexOf("from");
		if(index != -1){
				return "select count(*) " + hql.substring(index);
		}
		throw new SystemException("无效的HQL查询语句");
	}
	

	/**
	 * HQL 主要应用多表查询时的HQL查询
	 * PageResult
	 * 2011-5-11上午09:18:11
	 * author：李数
	 */
	@SuppressWarnings("unchecked")
	public PageResult searchPaginated(String counthql,String queryhql, Map filterMap){
		int page = (Integer) filterMap.get("page");
		int rows = (Integer) filterMap.get("rows");
		int start = rows * ( page - 1); //开始的记录
		//获取记录总数 
		Query query = getSession().createQuery(counthql);
		int total =((Number) query.uniqueResult()).intValue();
		
		//获取当前页的结果集
		query = getSession().createQuery(queryhql);
		
		query.setFirstResult(start);
		query.setMaxResults(rows);
		List<?> datas = query.list();
		
		PageResult pm = new PageResult();
		pm.setRowCount(total);
		pm.setData(datas);
		return pm;
	}
	
	/**
	 * 根据纯脆的SQL语句来查询
	 * PageResult
	 * 2011-5-11上午09:19:38
	 * author：李数
	 */
	public PageResult searchBySQL(String countsql,String querysql, Map filterMap){
		int page = (Integer) filterMap.get("page");
		int rows = (Integer) filterMap.get("rows");
		int start = rows * ( page - 1); //开始的记录
		//获取记录总数 
		Query query = getSession().createSQLQuery(countsql);
		List<Number> count = query.list();
		int total = Integer.parseInt(count.get(0).toString());
		
		//获取当前页的结果集
		query = getSession().createSQLQuery(querysql);
		query.setFirstResult(start);
		query.setMaxResults(rows);
		List<?> datas = query.list();
		PageResult pm = new PageResult();
		pm.setRowCount(total);
		pm.setData(datas);
		return pm; 
	}
	/**
	 * 根据纯脆的SQL语句来查询(去除select * from格式)
	 * PageResult
	 * 2011-5-11上午09:19:38
	 * author：李数
	 */
	public PageResult searchBySQLNoAll(String countsql,String querysql, Map filterMap){
		int page = (Integer) filterMap.get("page");
		int rows = (Integer) filterMap.get("rows");
		int start = rows * ( page - 1); //开始的记录
		//获取记录总数 
		Query query = getSession().createSQLQuery(countsql);
		List<Number> count = query.list();
		int total = Integer.parseInt(count.get(0).toString());
		
		//获取当前页的结果集
		query = getSession().createSQLQuery(querysql);
		query.setFirstResult(start);
//		query.setMaxResults(rows);
		List<?> datas = query.list();
		PageResult pm = new PageResult();
		pm.setRowCount(total);
		pm.setData(datas);
		return pm; 
	}
	
/*	*//**
	 * 获取用户信息
	 * SysFlowUser
	 * 2011-5-12上午11:31:49
	 * author：李数
	 *//*
	public SysFlowUser getSysFlowUser(String id){
		List<SysFlowUser> userList = this.getHibernateTemplate().find("from SysFlowUser user where user.userid=" + id);
		if(userList.size()>0){
			return userList.get(0);
		}
		return null;
	}*/
}
